package com.collect.senegal.zhiScene.zhiWidget.zhiFilterPerformance;

import android.content.Context;
import android.content.res.TypedArray;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiFilterPerformance.ZHIFilterLevelAdjuster;
import com.collect.senegal.zhiAdater.zhiFilterPerformance.ZHIFilterMonthAdjuster;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRFilterEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.senegal.zhiHelper.ZHIMainProcessHelper;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterAdminGroup.ZHIRangePicker;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeDateSelect;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeDay;
import com.collect.senegal.zhiUtils.StringUtil;
import com.collect.senegal.zhiUtils.ZHICalendarUtil;

import java.util.ArrayList;

public class ZHIFilterPerformanceMember extends RelativeLayout {

    public static final String TYPE_MONEY = "MONEY";
    public static final String TYPE_ORDER = "ORDER";

    private final Context mContext;

    private String mDataType = TYPE_MONEY;

    private String mLevel;
    private String mMonth;
    private String mPartRepay;

    private ZHIFilterLevelAdjuster mZHIFilterLevelAdjuster;
    private ZHIFilterMonthAdjuster mZHIFilterMonthAdjuster;

    private ArrayList<ZHIRFilterEntity> mLevelFilterArrayList;
    private ArrayList<ZHIRFilterEntity> mMonthFilterArrayList;

    private ZHIRangeDateSelect rds_due_date;
    private ZHIRangePicker rds_member;

    private ZHIFilterPerformanceClickListener mZHIFilterPerformanceClickListener;

    public ZHIFilterPerformanceMember(Context context) {
        this(context, null);
    }

    public ZHIFilterPerformanceMember(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIFilterPerformanceMember(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ZHIFilterPerformanceMember);
        mDataType = ta.getString(R.styleable.ZHIFilterPerformanceMember_dataType);
        ta.recycle();

        initView();
    }

    private void initView() {
        mMonth = ZHICalendarUtil.getMonth() + "";
        mLevel = ZHIMainProcessHelper.getInstance().getLevel();

        View mView = View.inflate(mContext, R.layout.view_filter_performance_member, this);
        rds_due_date = mView.findViewById(R.id.rds_due_date);
        rds_member = mView.findViewById(R.id.rds_member);

        TextView tv_reset = mView.findViewById(R.id.tv_reset);
        tv_reset.setOnClickListener(v -> {
            String curMonth = ZHICalendarUtil.getMonthSimpleEng();
            for (ZHIRFilterEntity zhirFilterEntity1 : mMonthFilterArrayList) {
                zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equals(curMonth));
            }
            mMonth = ZHICalendarUtil.getMonth() + "";
            mZHIFilterMonthAdjuster.notifyDataSetChanged();
        });

        RelativeLayout rl_close_bg = mView.findViewById(R.id.rl_close_bg);
        rl_close_bg.setOnClickListener(v -> {
            if (mZHIFilterPerformanceClickListener != null) {
                mZHIFilterPerformanceClickListener.onCloseClick();
            }
        });

        TextView tv_submit = mView.findViewById(R.id.tv_submit);
        tv_submit.setOnClickListener(v -> {
            if (mZHIFilterPerformanceClickListener != null) {
                mZHIFilterPerformanceClickListener.onConfirmClick();
            }
        });

        ///////////////////////
        RecyclerView rc_level_list = mView.findViewById(R.id.rc_level_list);
        initLevelDataSource();

        mZHIFilterLevelAdjuster = new ZHIFilterLevelAdjuster();
        mZHIFilterLevelAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {

        });
        rc_level_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_level_list.setAdapter(mZHIFilterLevelAdjuster);
        mZHIFilterLevelAdjuster.setNewData(mLevelFilterArrayList);

        ///////////////////////
        //暂时隐藏--start
        RecyclerView rc_month_list = mView.findViewById(R.id.rc_month_list);
        initMonthDataSource();

        mZHIFilterMonthAdjuster = new ZHIFilterMonthAdjuster();
        mZHIFilterMonthAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                mMonth = (position + 1) + "";
                for (ZHIRFilterEntity zhirFilterEntity1 : mMonthFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase(zhirFilterEntity.getLevel()));
                }
                mZHIFilterMonthAdjuster.notifyDataSetChanged();
            }
        });
        rc_month_list.setLayoutManager(new GridLayoutManager(mContext, 4));
        rc_month_list.setAdapter(mZHIFilterMonthAdjuster);
        mZHIFilterMonthAdjuster.setNewData(mMonthFilterArrayList);
        //暂时隐藏--end
        ///////////////////////

        ////////////////////////////////////
        initDueDateDataSource();

        ////////////////////////////////////
        rds_member.setDataSource(initUserDataSource());
        rds_member.updateCompare(false, ZHIMainProcessHelper.getInstance().getLoginEntity().getUserName());
        rds_member.mOnlyShowDefaultValue = true;
    }

    private void initLevelDataSource() {
        try {
            mDataType = TYPE_MONEY;
            mLevel = ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLeven();

            if (mLevelFilterArrayList != null) {
                mLevelFilterArrayList.clear();
            } else {
                mLevelFilterArrayList = new ArrayList<>();
            }

            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLeven());
            zhirFilterEntity.setChecked(true);
            mLevelFilterArrayList.add(zhirFilterEntity);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initMonthDataSource() {
        try {
            String[] months = new String[]{
                    "Jan", "Feb", "Mar", "Apr",
                    "May", "Jun", "July", "Aug",
                    "Sep", "Oct", "Nov", "Dec"};
            String curMonth = ZHICalendarUtil.getMonthSimpleEng();

            if (mMonthFilterArrayList != null) {
                mMonthFilterArrayList.clear();
            } else {
                mMonthFilterArrayList = new ArrayList<>();
            }

            for (int i = 0; i < months.length; i++) {
                ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
                zhirFilterEntity.setLevel(months[i]);
                zhirFilterEntity.setChecked(months[i].equalsIgnoreCase(curMonth));
                mMonthFilterArrayList.add(zhirFilterEntity);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ArrayList<ZHIRangeDay> initUserDataSource() {
        ArrayList<ZHIRangeDay> arrayList = new ArrayList<>();
        try {
//            ArrayList<String> userList = CFMainProcessHelper.getInstance().getLoginEntity().getGroupUserLoginNameList();
//            for (int i = 0; i < userList.size(); i++) {
//                ZHIRangeDay zhiRangeDay = new ZHIRangeDay();
//                zhiRangeDay.setRangeId(i + "");
//                zhiRangeDay.setDayTitle(userList.get(i));
//                arrayList.add(zhiRangeDay);
//            }

            ArrayList<ZHIRGroupUserEntity> userList = ZHIMainProcessHelper.getInstance().getGroupUserEntityArrayList();
            for (int i = 0; i < userList.size(); i++) {
                ZHIRGroupUserEntity zhirGroupUserEntity = userList.get(i);
                ZHIRangeDay zhiRangeDay = new ZHIRangeDay();
                zhiRangeDay.setRangeId(i + "");
                zhiRangeDay.setDayTitle(zhirGroupUserEntity.getLoginName());
                arrayList.add(zhiRangeDay);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return arrayList;
    }

    private void initDueDateDataSource() {
        try {
            int month = ZHICalendarUtil.getMonth();
            int cday = ZHICalendarUtil.getDay();

            String monthString = month >= 10 ? String.valueOf(month) : ("0" + month);
            String cdayString = cday >= 10 ? String.valueOf(cday) : ("0" + cday);

            String startFormatString = ZHICalendarUtil.getYear() + "-" + monthString + "-" + "01";
            rds_due_date.updatePromisedDueTimeStart(false, ZHICalendarUtil.getDateFromFormatString(startFormatString));

            String endFormatString = ZHICalendarUtil.getYear() + "-" + monthString + "-" + cdayString;
            rds_due_date.updatePromisedDueTimeEnd(false, ZHICalendarUtil.getDateFromFormatString(endFormatString));

            rds_due_date.mOnlyShowDefaultValue = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateUserDataSource() {
        rds_member.setDataSource(initUserDataSource());
    }

    public String getLevel() {
        try {
            String tempLevel = StringUtil.safeString(mLevel);
            //order需要转M
            tempLevel = tempLevel.replaceAll("\\+", "");
            tempLevel = tempLevel.replaceAll("B", "M");

            return tempLevel;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "M1";
    }

    public String getMonth() {
        return StringUtil.safeString(mMonth);
    }

    public String getDudStartDate() {
        return StringUtil.safeString(rds_due_date.getStartDate());
    }

    public String getDueEndDate() {
        return StringUtil.safeString(rds_due_date.getEndDate());
    }

    public String getMember() {
        return rds_member.getCompare();
    }

    public void setZHIPerformanceFilterClickListener(ZHIFilterPerformanceClickListener clickListener) {
        this.mZHIFilterPerformanceClickListener = clickListener;
    }
}
